class Solution {
public:
    bool backspaceCompare(string s, string t) {
        return backspace(s) == backspace(t);
    }

    string backspace(string &s){
        int slow = 0;
        for(int fast = 0; fast < s.size(); ++fast){
            if(s[fast] == '#'){
                slow = max(--slow, 0);
            } else {
                s[slow] = s[fast];
                ++slow;
            }
        }
        
        return s.substr(0,slow);
    }
};